//
//  expressDataViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/16.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "expressDataViewController.h"

static NSString *const Xcell = @"Xcell";

@interface expressDataViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, retain)NSMutableArray *BiaoArray;
@property(nonatomic, retain)UITableView *myTable;
@property(nonatomic, retain)expressTou *modelT;
@property(nonatomic, assign)float heightD;

@property(nonatomic, assign)NSInteger heightW;

@end

@implementation expressDataViewController

-(void)dealloc{
    [_myTable release];
    [_model release];
    [_BiaoArray release];
    
    [_labelN release];
    [_labelZ release];
    [_labelT release];
    [_imageT release];
    
    
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeHeight" object:nil];
    
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(changeHeight:) name:@"changeHeight" object:nil];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor yellowColor];
    self.BiaoArray = [NSMutableArray array];
    
    [self getData];
    [self creatTable];
 
    
    // Do any additional setup after loading the view.
}

-(void)creatTable{
    self.myTable = [[UITableView alloc] initWithFrame:CGRectMake(0, -200, self.view.frame.size.width, self.view.frame.size.height+200) style:UITableViewStylePlain];
    self.myTable.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTable];
    self.myTable.delegate = self;
    self.myTable.dataSource = self;
    [self.myTable registerClass:[expressDataTableViewCell class] forCellReuseIdentifier:Xcell];
    self.myTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.BiaoArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    expressDataTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Xcell forIndexPath:indexPath];
    expressBiao *model = [[expressBiao alloc] init];
    model = [self.BiaoArray objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

-(void)changeHeight:(NSNotification *)noti{
    self.heightW = [[noti object] integerValue];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
//    NSLog(@"%lu",_heightW);
    
    
    return _heightW+100;
    
    
}

#pragma make 取数据
-(void)getData{

    NSString *str = [NSString stringWithFormat:@"http://cont.app.autohome.com.cn/autov5.0.0/content/News/fastnewscontent-n%@-lastid0-o1.json",self.model.id
                     ];
    [ZYNetworkHandle GetDataWithURL:str dic:nil success:^(id responseObject) {
        
//头视图数据
        NSDictionary *result = [responseObject objectForKey:@"result"];
        NSDictionary *newsData = [result objectForKey:@"newsdata"];
        self.modelT = [[expressTou alloc] init];
        [self.modelT yy_modelSetWithDictionary:newsData];
        
        [self.modelT release];
        
//tableView数据
        NSMutableArray *messagelist = [NSMutableArray array];
        messagelist = [result objectForKey:@"messagelist"];
        
        
        for (NSDictionary *dic in messagelist) {
            expressBiao *biao = [[expressBiao alloc] init];
            [biao yy_modelSetWithDictionary:dic];
            [self.BiaoArray addObject:biao];
            
            [biao release];
        }
        
    //头视图赋值
        [self creatTou];
        [self.imageT sd_setImageWithURL:[NSURL URLWithString:_modelT.img]];
        self.labelT.text = _modelT.title;
        self.labelZ.text = [NSString stringWithFormat:@"编辑:  %@",_modelT.newsauthor];
        self.labelN.text = [NSString stringWithFormat:@"%@人浏览",_modelT.reviewcount];
        
        [self.myTable reloadData];
        
        
    } filed:^(NSError *error) {
        
        
    }];
}
#pragma make 创建头视图
-(void)creatTou{

    self.imageT = [[UIImageView alloc] init];
    self.imageT.frame = CGRectMake(0, -200, self.view.frame.size.width, self.view.frame.size.height/6+250);
    [self.view addSubview:self.imageT];
    self.imageT.backgroundColor = [UIColor clearColor];
    self.myTable.tableHeaderView = self.imageT;
//打开背景图的用户交互
    self.imageT.userInteractionEnabled = YES;
    
   FengButton *buttonH = [FengButton buttonWithType:UIButtonTypeCustom frame:CGRectMake(10, 230, 40, 30) title:nil image:[UIImage imageNamed:@""] color:[UIColor yellowColor] handler:^(UIButton *sender) {
#pragma make button点击方法
       [self dismissViewControllerAnimated:YES completion:^{
           
       }];
    }];
    [self.imageT addSubview:buttonH];
//标题
    self.labelT = [[UILabel alloc] initWithFrame:CGRectMake(10, buttonH.frame.size.height+buttonH.frame.origin.y+15, self.view.frame.size.width-20, 45)];
    [self.imageT addSubview:self.labelT];
    self.labelT.backgroundColor = [UIColor clearColor];
    self.labelT.font = [UIFont systemFontOfSize:22];
    self.labelT.textColor = [UIColor whiteColor];
    
//作者
    self.labelZ = [[UILabel alloc] initWithFrame:CGRectMake(10, self.labelT.frame.size.height+self.labelT.frame.origin.y+15, self.view.frame.size.width/4, 25)];
    self.labelZ.backgroundColor = [UIColor clearColor];
    [self.imageT addSubview:self.labelZ];
    self.labelZ.textColor = [UIColor whiteColor];
    
//浏览人数
    self.labelN = [[UILabel alloc] initWithFrame:CGRectMake(self.labelZ.frame.size.width+self.labelZ.frame.origin.x+20, self.labelT.frame.size.height+self.labelT.frame.origin.y+15, self.view.frame.size.width/3, 25)];
    self.labelN.backgroundColor = [UIColor clearColor];
    [self.imageT addSubview:self.labelN];
    self.labelN.textColor = [UIColor whiteColor];
    
    
    [self.imageT release];
    [self.labelT release];
    [self.labelZ release];
    [self.labelN release];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
